package com.alexmaodali.controller;

import com.alexmaodali.domain.dto.UserQueryDTO;
import com.alexmaodali.domain.po.PageBean;
import com.alexmaodali.domain.po.User;
import com.alexmaodali.service.UserService;
import com.alexmaodali.utils.Result;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.Currency;
import java.util.List;


@Slf4j
@RestController
@RequestMapping("/user")
@RequiredArgsConstructor
public class UserController {
    final private UserService userService;

    /**
     * 查询全部
     *
     * @return
     */
    @GetMapping("/getAll")
    public Result getAll() {
        log.info("\n\n查询全部\n\n");
        List<User> userList = userService.getAll();
        return Result.success(userList);
    }

    /**
     * 根据id查询
     *
     * @param id
     * @return
     */
    @GetMapping("/getById")
    public Result getById(Integer id) {
        log.info("\n\n根据id查询:id=>{}\n\n", id);
        User user = userService.getById(id);
        return Result.success(user);
    }

    /**
     * 根据id删除
     *
     * @param id
     * @return
     */
    @DeleteMapping("/deleteById/{id}")
    public Result deleteById(@PathVariable Integer id) {
        log.info("\n\n根据id删除:ID=>{}\n\n", id);
        userService.deleteById(id);
        return Result.success();
    }

    /**
     * 根据id禁用
     *
     * @param id
     * @return
     */
    @PostMapping("/disableById/{id}")
    public Result disableById(@PathVariable Integer id) {
        log.info("\n\n根据id禁用:ID=>{}\n\n", id);
        userService.disableById(id);
        return Result.success();
    }

    /**
     * 根据id启用
     *
     * @param id
     * @return
     */
    @PostMapping("/enableById/{id}")
    public Result enableById(@PathVariable Integer id) {
        log.info("\n\n根据id启用:ID=>{}\n\n", id);
        userService.enableById(id);
        return Result.success();
    }

    /**
     * 新增用户
     *
     * @param user
     * @return
     */
    @PostMapping("/insertUser")
    public Result insertUser(@RequestBody User user) {
        log.info("\n\n新增用户:user=>{}\n\n", user);
        userService.insertUser(user);
        return Result.success();
    }

    /**
     * 更新用户
     *
     * @param user
     * @return
     */
    @PostMapping("/updateUser")
    public Result updateUser(@RequestBody User user) {
        log.info("\n\n更新用户:user=>{}\n\n", user);
        userService.updateUser(user);
        return Result.success();
    }

    /**
     * 分页查询
     *
     * @return
     */
    @GetMapping("/page")
    public Result page(@RequestParam(value = "pageNum", defaultValue = "1") Integer currentPage,
                       @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                       UserQueryDTO userQueryDTO) {
        log.info("\n\npageNum:{},pageSize:{}\n\n", currentPage, pageSize);
        PageBean pageBean =  userService.page(currentPage, pageSize, userQueryDTO);
        return Result.success(pageBean);
    }

    /**
     * 综合删除
     */
    @DeleteMapping("/delete/{ids}")
    public Result delete(@PathVariable List<Integer> ids) {
        log.info("\n\n删除用户:id=>{}\n\n", ids);
        userService.delete(ids);
        return Result.success();
    }
}
